package de.htwg_konstanz.ebus.framework.view;

import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import de.htwg_konstanz.ebus.framework.impEx.IImporter;
import de.htwg_konstanz.ebus.framework.impEx.XmlImporter;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.BMECAT;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogService;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceFault;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServicePortType;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceRequest;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceResponse;

public class WebserviceImportAction implements IAction {
	public static final String ACTION_WEBSERVICE_IMPORT = "webservice_import";
		
	public boolean accepts(String actionName) {
		return actionName.equalsIgnoreCase(ACTION_WEBSERVICE_IMPORT);
	}

	public String execute(HttpServletRequest request, HttpServletResponse response, ArrayList<String> errorList) {
		request.getSession(true).setAttribute("importStatus", null);
		request.getSession(true).setAttribute("faultStatus", null);
		
		ProductCatalogService pcs = new ProductCatalogService();
		ProductCatalogServicePortType portType = pcs.getProductCatalogServicePort();
		
		ProductCatalogServiceRequest serviceRequest = new ProductCatalogServiceRequest();
		serviceRequest.setUserName(request.getParameter("username"));
		serviceRequest.setPassword(request.getParameter("password"));
		
		try {
			ProductCatalogServiceResponse pcsr = portType.getProductCatalog(serviceRequest);
			BMECAT bmecat = pcsr.getBMECAT();
			
            try {
		        StringWriter sw = new StringWriter();
		        JAXBContext jc = JAXBContext.newInstance("de.htwg_konstanz.ebus.framework.webservices.productcatalog");
		        Marshaller marshaller = jc.createMarshaller();
		        marshaller.marshal(bmecat, new File("resources/upload/importedCatalog.xml"));
		        
		        IImporter xmlImporter = new XmlImporter("resources/import/bmecat_new_catalog_1_2_simple_without_NS.xsd", 
		        										"resources/upload/importedCatalog.xml");
		        
		        System.out.println("--> " + xmlImporter.isValid());
		        
		        //if (xmlImporter.isValid()) {
		        	xmlImporter.performImport();
		        //}
		        
		        ImpExStatusModel importStatus = xmlImporter.getStatusModel();
		    	request.getSession(true).setAttribute("importStatus", importStatus);
		    	
		    		
            } catch(Exception e) {
            	e.printStackTrace();
            }
	    
		} catch (ProductCatalogServiceFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.getSession(true).setAttribute("faultStatus", e);
			System.out.println(e.getFaultInfo());
		}
		
		return "importResult.jsp";
	}

}
